﻿using LaserAutoTest.Application.Models;
using LaserAutoTest.Application;
using Prism.Commands;
using Prism.Mvvm;
using System.Windows;

namespace LaserAutoTest.UI.ViewModels.Logins
{
    public class LoginViewModel : BindableBase
    {
        private IUserService _userService;
       
        //public DelegateCommand CancelCommand { get; set; }

        public LoginViewModel(IUserService userService)
        {
            _userService = userService;
            LoginCommand = new DelegateCommand<object>(Login);
            CloseCommand = new DelegateCommand<object>(Close);
            //CancelCommand = new DelegateCommand(Cancel);
        }

        #region binding

        private UserModel _user = new UserModel();
        public UserModel User
        {
            get { return _user; }
            set
            {
                SetProperty(ref _user, value);
            }
        }


        private Visibility _showProgress = Visibility.Collapsed;

        public Visibility ShowProgress
        {
            get { return _showProgress; }
            set
            {
                SetProperty(ref _showProgress, value);
                LoginCommand.RaiseCanExecuteChanged();
            }
        }

        private string _errorMessage;

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                SetProperty(ref _errorMessage, value);
            }
        }
        private Visibility _showError;

        public Visibility ShowError
        {
            get { return _showError; }
            set
            {
                SetProperty(ref _showError, value);
            }
        }

        #endregion

        #region command

        public DelegateCommand<object> LoginCommand { get; set; }
        public DelegateCommand<object> CloseCommand { get; set; }


        private void Login(object obj)
        {
            if (GlobalValues.UserInfo != null)
                User = GlobalValues.UserInfo;
            this.ShowProgress = Visibility.Visible;
            this.ShowError = Visibility.Collapsed;

            if (string.IsNullOrEmpty(User.JobNumber))
            {
                this.ErrorMessage = "请输工号";
                this.ShowError = Visibility.Visible;
                this.ShowProgress = Visibility.Collapsed;
                //MessageBox.Show("工号不可为空", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            if (string.IsNullOrEmpty(User.Password))
            {
                this.ErrorMessage = "请输入密码";
                this.ShowError = Visibility.Visible;
                this.ShowProgress = Visibility.Collapsed;

                //MessageBox.Show("密码不可为空", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            var canLogin = _userService.CanUserLogin(User.JobNumber, User.Password);
            if (canLogin)
            {
                GlobalValues.UserInfo = User;
                this.ShowProgress = Visibility.Collapsed;
                MessageBox.Show("登陆成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                (obj as Window).DialogResult = false;
                return;
            }
            else
            {
                this.ErrorMessage = "用户名密码错误";
                this.ShowError = Visibility.Visible;
                this.ShowProgress = Visibility.Collapsed;
                //MessageBox.Show("用户名密码错误", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
        }
        private void Close(object obj)
        {
            (obj as Window).DialogResult = false;
        }
        //private void Cancel(object o)
        //{
        //    (o as Window).DialogResult = false ;
        //}

        #endregion

    }
}
